<template>
    <div class="video-box" v-loading="loading">
        <div class="select-box">
            <el-select v-model="status" @change="statusChange">
                <el-option label="全部" :value="-4"></el-option>
                <el-option label="已删除" :value="-3"></el-option>
                <el-option label="下架" :value="-2"></el-option>
                <el-option label="自动审核失败" :value="-1"></el-option>
                <el-option label="待审核（阿里审核中)" :value="0"></el-option>
                <el-option label="自动审核通过" :value="1"></el-option>
                <el-option label="人工审核通过" :value="2"></el-option>
            </el-select>
        </div>

        <el-table
            :data="tableData"
            border
            style="width: 100%">
            <el-table-column
                fixed
                prop="videoId"
                label="ID">
            </el-table-column>
            <el-table-column
                width="180"
                label="视频">
                <template slot-scope="scope">
                    <videoBox :url="scope.row.videoUrl" :time="scope.row.duration" :img="scope.row.videoCoverUrl"/>
                </template>
            </el-table-column>
            <el-table-column
                width="180"
                label="标题">
                <template slot-scope="scope">
                     <span>{{scope.row.videoTitle || '-'}}</span>
                </template>
            </el-table-column>
            <el-table-column
                width="180"
                label="关联游戏">
                <template slot-scope="scope">
                     <span v-if="scope.row.gameName" @click="goGameDetail(scope.row.gameId)" class="table-btn">{{scope.row.gameName}}</span>
                     <span v-else>-</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="date"
                label="首页推荐">
                <template slot-scope="scope">
                     <span v-if="scope.row.showHome === 1">是</span>
                     <span v-else>-</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="publishTime"
                width="108"
                :sortable="true"
                label="发布时间">
                <template slot-scope="scope">
                     <div style="text-align:center;">{{initTime(scope.row.publishTime)}}</div>
                </template>
            </el-table-column>
            <el-table-column
                prop="auditStatus"
                width="120"
                :sortable="true"
                label="状态">
                <template slot-scope="scope">
                    <div style="color: #F56C6C;" v-if="scope.row.auditStatus === -3">已删除</div>
                    <div style="color: #F56C6C;" v-else-if="scope.row.auditStatus === -2">
                        <div class="point red-point"></div>
                        <span>下架</span>
                    </div>
                    <div style="color: #F56C6C;" v-else-if="scope.row.auditStatus === -1">自动审核失败</div>
                    <div v-else-if="scope.row.auditStatus === 0">待审核（阿里审核中)</div>
                    <div style="color:#67C23A;" v-else-if="scope.row.auditStatus === 1">自动审核通过</div>
                    <div style="color:#67C23A;" v-else-if="scope.row.auditStatus === 2">
                        <span class="point"></span>
                        <span>人工审核通过</span>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="clickZanCount"
                width="105"
                :sortable="true"
                label="点赞数">
                <template slot-scope="scope">
                     <span>{{scope.row.clickZanCount || '0'}}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="commentNum"
                width="105"
                :sortable="true"
                label="回复数">
                <template slot-scope="scope">
                     <span>{{scope.row.commentNum || '0'}}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="rewardUserNum"
                width="105"
                :sortable="true"
                label="打赏人数">
                <template slot-scope="scope">
                     <span>{{scope.row.rewardUserNum || '0'}}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="rewardCoinNum"
                width="105"
                :sortable="true"
                label="打赏金币">
                <template slot-scope="scope">
                     <span>{{scope.row.rewardCoinNum || '0'}}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="playCount"
                :sortable="true"
                width="105"
                label="播放数量">
                <template slot-scope="scope">
                     <span>{{scope.row.playCount || '0'}}</span>
                </template>
            </el-table-column>
            <el-table-column
                width="150"
                label="举报">
                <template slot-scope="scope">
                    <div v-if="scope.row.report && scope.row.report.length">
                        <div v-for="(item, index) in scope.row.report" :key="index">
                            <span>{{item.text}}</span>
                            <span v-if="item.num">x{{item.num}}</span>
                        </div>
                    </div>
                    <div v-else>-</div>
                </template>
            </el-table-column>
            <el-table-column
                fixed="right"
                label="操作"
                width="100">
                <template slot-scope="scope">
                    <div v-if="scope.row.auditStatus >= 1"><el-button @click="beforeRefusedUser(scope.row)" type="text" size="small" style="color: #F56C6C;">下架</el-button></div>
                    <!-- 下架、自动审核失败 才有删除 -->
                    <div v-if="scope.row.auditStatus === -2 || scope.row.auditStatus === -1">
                        <div><el-button @click="delVideo(scope.row.videoid, scope.$index)" type="text" size="small" style="color: #F56C6C;">删除</el-button></div>
                    </div>
                    <!-- 下架、自动审核通过 才能通过 -->
                    <div v-if="scope.row.auditStatus === -2 || scope.row.auditStatus === 1 || scope.row.auditStatus === -1"><el-button @click="changeVideoStatus(scope.row, 2)" type="text" size="small" style="color: #67C23A;">通过</el-button></div>
                    <div><el-button type="text" size="small" @click="goVideoDetail(scope.row.videoId)">查看详情</el-button></div>
                </template>
            </el-table-column>
        </el-table>

        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>

        <!-- 拒绝用户 -->
        <el-dialog
            class="show-refused-user-box"
            title="下架原因"
            :close-on-click-modal="false"
            :show-close="false"
            :visible.sync="showRefusedUser"
            width="600px">
            <el-radio-group v-model="radioTxt">
                <el-radio-button class="radio-item" label="非游戏">非游戏</el-radio-button>
                <el-radio-button class="radio-item" label="质量低">质量低</el-radio-button>
                <el-radio-button class="radio-item" label="不宜展示">不宜展示</el-radio-button>
                <el-radio-button class="radio-item" label="优化标题">优化标题</el-radio-button>
                <el-radio-button class="radio-item" label="关联游戏错误">关联游戏错误</el-radio-button>
                <el-radio-button class="radio-item" label="关联视频类型错误">关联视频类型错误</el-radio-button>
                <el-radio-button class="radio-item" label="黑屏">黑屏</el-radio-button>
            </el-radio-group>
            <el-input
                ref="textarea"
                type="textarea"
                :rows="2"
                placeholder="请输入下架原因"
                v-model="textarea">
            </el-input>
            <span slot="footer" class="dialog-footer">
                <el-button size="mini" @click="showRefusedUser = false">取 消</el-button>
                <el-button size="mini" type="primary" @click="refusedUser">确定下架</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { formatDate } from '@/utils/utils'
export default {
    data () {
        return {
            tableData: [],
            loading: false,
            status: -4,
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0,
            showRefusedUser: false, // 拒绝用户
            radioTxt: '非游戏',
            textarea: ''
        }
    },

    props: ['detail'],

    created () {
        if (this.detail && this.detail.userId) {
            this.getUserPublishVideo()
        }
    },

    watch: {
        textarea (val) {
            if (val) {
                this.radioTxt = ''
            }
        },
        radioTxt (val) {
            if (val) {
                this.textarea = ''
            }
        }
    },

    methods: {
        async getUserPublishVideo () {
            if (this.loading) return
            this.loading = true
            let params = {
                userId: this.detail.userId,
                pageNum: this.pageNum,
                pageSize: this.pageSize
            }
            if (this.status !== -4) {
                params.status = this.status
            }

            let res = await this.$http.get('/user/getUserPublishVideo', {
                params
            })
            if (res.code === '0') {
                this.tableData = res.data
                this.total = res.count || res.data.length
            }
            this.loading = false
        },

        initTime (time) {
            if (time) {
                return formatDate(new Date(time), 'YYYY-MM-DD HH:MM')
            } else {
                return '-'
            }
        },

        // 查看游戏详情
        goGameDetail (id) {
            const { href } = this.$router.resolve({
                path: '/game/gameDetail',
                query: {
                    id
                }
            })
            window.open(href, '_blank')
        },

        // 查看视频详情
        goVideoDetail (id) {
            this.$router.push({
                path: '/video/videoDetail',
                query: {
                    id
                }
            })
        },

        // 更改视频状态
        async changeVideoStatus (item, status) {
            let params = {
                videoId: item.videoId,
                status
            }
            let res = await this.$http.post('/video/updateGameVideoStatus', params)
            if (res.code === '0') {
                item.auditStatus = status
                this.$message({
                    type: 'success',
                    message: '修改成功'
                })
            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
        },

        // 拒绝用户
        beforeRefusedUser (item) {
            this.currentItem = item
            this.showRefusedUser = true
        },
        // 拒绝用户
        async refusedUser () {
            if (!this.radioTxt && !this.textarea) {
                this.$message({
                    type: 'error',
                    message: '请输入或选择驳回理由!'
                })
                return
            }
            let params = {
                videoId: this.currentItem.videoId,
                status: -2,
                reason: this.radioTxt || this.textarea
            }
            let res = await this.$http.post('/video/updateGameVideoStatus', params)
            if (res.code === '0') {
                this.$message({
                    type: 'success',
                    message: '已下架'
                })
                this.currentItem.auditStatus = -2
                this.currentItem.auditReason = params.reason
            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
            this.showRefusedUser = false
        },

        sizeChange (val) {
            this.pageNum = 1
            this.pageSize = val
            this.getUserPublishVideo()
        },

        pageChange (val) {
            this.pageNum = val
            this.getUserPublishVideo()
        },

        // 切换查询状态
        statusChange (val) {
            this.pageNum = 1
            this.getUserPublishVideo()
        }
    }
}
</script>

<style lang="scss" scoped>
    .video-box {
        .select-box {
            margin-bottom: 10px;
        }

        .table-btn {
            color: #409EFF;
            cursor: pointer;
        }
        .point {
            display:inline-block;
            width:10px;
            height:10px;
            border-radius: 50%;
            background-color:#67C23A;
            margin-right:3px;
        }
        .red-point {
            background-color: #F56C6C;
        }

        .tools {
            margin-top: 10px;
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }

        .show-refused-user-box {
            /deep/ .el-dialog__body {
                padding: 0 20px;
            }
            .radio-item {
                border: 1px solid #DCDFE6;
                margin-right: 10px;
                margin-bottom: 10px;
                /deep/ .el-radio-button__inner {
                    border: none;
                }
            }
        }
    }
</style>
